package com.HW;

import java.util.HashMap;

/**
 * @ClassName : TFullAndOccCharSet
 * @Author : kele
 * @Date: 2023/10/22 14:12
 * @Description : 全量和已占用字符集
 */
public class TFullAndOccCharSet {

    public static void main(String[] args) {

        String s = "a:3,b:5,c:2@a:1,b:2";
        handle(s);

    }

    public static void handle(String str) {

        String[] split = str.split("@");
        StringBuilder builder = new StringBuilder();
        if(split.length == 0){
            System.out.println("");
        } else if(split.length == 1) {
            System.out.println(split[0]);
        }else{

            HashMap<String, Integer> map = new HashMap<>();
            for (String s : split[0].split(",")) {
                String[] charSet = s.split(":");
                map.put(charSet[0],Integer.parseInt(charSet[1]));
            }

            for (String s : split[1].split(",")) {
                String[] charSet = s.split(":");
                String key = charSet[0];
                int value = map.get(key) - Integer.parseInt(charSet[1]);
                if(value == 0){
                    map.remove(key);
                }else{
                    map.put(key,value);
                }
            }

            map.entrySet().forEach(x-> builder.append(x.getKey()).append(":").append(x.getValue()).append(","));

            System.out.println(builder.substring(0,builder.length()-1));

        }


    }
}
